package cn.elane.elastic.global.config;

import cn.elane.elastic.global.jdbc.JdbcSource;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 默认数据源配置类
 *
 * @author Chenyu
 */
@Configuration
public class DataSourceConfig {

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource initDefaultDataSource() {
		DataSource dataSource;
		try {
			dataSource = new DruidDataSource();
			boolean flag = JdbcSource.addDataSource(JdbcSource.getDefaultAlias(), dataSource);
			if (!flag) {
				throw new RuntimeException("默认数据源异常！");
			}
			return dataSource;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			dataSource = null;
		}
	}
}
